package cn.school.controller.stu.evaluation;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.stu.evaluation.entity.StuEvaIndicator;
import cn.school.stu.evaluation.service.StuEvaIndicatorService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/5/24 11:03
 * @description: 考评指标控制器
 */
@RestController
@Api(tags = "StuEvaIndicatorController => 考评指标控制器")
@RequestMapping("stu/evaluation/indicator")
public class StuEvaIndicatorController {

    @Autowired
    private StuEvaIndicatorService indicatorService;

    @ApiOperation("createStuEvaIndicator => 新增考评指标")
    @PostMapping("/createStuEvaIndicator")
    public CommonResult createStuEvaIndicator(@RequestBody StuEvaIndicator stuEvaIndicator){
        if("1".equals(stuEvaIndicator.getType())){
            if(stuEvaIndicator.getPid() == null){
                return CommonResult.failed("类型为二级考评指标时，上级指标不能为空");
            }
        }
        int num = indicatorService.createStuEvaIndicator(stuEvaIndicator);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("findFirstLevelIndicator => 查询一级考评指标")
    @GetMapping("/findFirstLevelIndicator")
    public CommonResult findFirstLevelIndicator(){
        List<StuEvaIndicator> list = indicatorService.findFirstLevelIndicator();
        return CommonResult.success(list);
    }

    @ApiOperation("findStuEvaIndicatorPageList => 分页查询")
    @PostMapping("/findStuEvaIndicatorPageList")
    public CommonResult<List<StuEvaIndicator>> findStuEvaIndicatorPageList(@RequestBody Map<String,Object> queryParam){
        List<StuEvaIndicator> pageList = indicatorService.findStuEvaIndicatorPageList(queryParam);
        return CommonResult.success(pageList);
    }

    @GetMapping("/findStuEvaIndicatorList/{id}")
    public CommonResult<List<StuEvaIndicator>> findStuEvaIndicatorList(@PathVariable String id){
        List<StuEvaIndicator> pageList = indicatorService.findStuEvaIndicatorList(id);
        return CommonResult.success(pageList);
    }

    @ApiOperation("deleteStuEvaIndicatorById => 删除")
    @GetMapping("/deleteStuEvaIndicatorById/{id}")
    public CommonResult deleteStuEvaIndicatorById(@PathVariable("id")Long id){
        int num = indicatorService.deleteStuEvaIndicatorById(id);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }
    
    @ApiOperation("getChildrenIndicatorById => 通过id获取二级指标")
    @GetMapping("getChildrenIndicatorById/{id}")
    public CommonResult< Map<String, List<StuEvaIndicator>>> getChildrenIndicatorById(@PathVariable("id") String id) {
        Map<String, List<StuEvaIndicator>> evaIndicatorList = indicatorService.getChildrenIndicatorById(id);
        return CommonResult.success(evaIndicatorList);
    }
}
